layui.config({
	base : "js/"
}).use(['form','layer','jquery','laydate','tree'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
		laytree = layui.tree,
		$ = layui.jquery;

	//解决layui低版本，switch开关关闭不能传值到后台的bug
	form.on('switch', function(data) {
		$(data.elem).attr('type', 'hidden').val(this.checked ? true : false);
	});

	//查询树
	$.get("/author/getTree.do",function(data){
		//console.log(data);
		laytree({
			elem: '#authorTree' //传入元素选择器
			,nodes:data,
			click: function (node) {
				var $select = $($(this)[0].elem).parents(".layui-form-select");
				$select.removeClass("layui-form-selected").find(".layui-select-title span").html(node.name).end().find("input:hidden[name='parent.id']").val(node.id);
			}
		});
		$(".downpanel").on("click", ".layui-select-title", function (e) {
			$(".layui-form-select").not($(this).parents(".layui-form-select")).removeClass("layui-form-selected");
			$(this).parents(".downpanel").toggleClass("layui-form-selected");
			layui.stope(e);
		}).on("click", "dl i", function (e) {
			layui.stope(e);
		});
		$(document).on("click", function (e) {
			$(".downpanel").removeClass("layui-form-selected");
		});
	});


	form.on("submit(add)",function(data){

		//弹出loading
		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url:"/author/insert.do",
			data:data.field,
			type:"post",
			success:function(data){
				top.layer.close(index);
				top.layer.msg("保存成功！");
				layer.closeAll("iframe");
				//刷新父页面
				parent.location.reload();
		    },
			error:function(){
				top.layer.close(index);
				top.layer.msg("添加失败！");
				layer.closeAll("iframe");

			}
		})
		return false;
	})
})
